package com.ym.controller.soso;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import com.ym.base.enums.FlagEnum;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.ImportJsonUtil;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.domain.entity.HospitalEntity;
import com.ym.medical.domain.entity.MedicalCentreEntity;
import com.ym.medical.domain.param.DataMainPicpParam;
import com.ym.medical.domain.param.HospitalJointParam;
import com.ym.medical.domain.param.HospitalPcUpdateParam;
import com.ym.medical.domain.param.HospitalQueryParam;
import com.ym.medical.domain.param.HospitalRankEditParam;
import com.ym.medical.domain.vo.HospitalDetailVo;
import com.ym.medical.domain.vo.HospitalVo;
import com.ym.medical.service.IHospitalSV;
import com.ym.medical.service.IMedicalCentreSV;
import com.ym.soso.domain.entity.ChinaMedicineEntity;
import com.ym.soso.domain.entity.PensionInfoEntity;
import com.ym.soso.service.IPensionInfoSV;
import com.ym.soso.domain.param.RegistryWayParam;
import com.ym.soso.service.IChinaMedicineSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author: 张文博
 * @version: 2019年11月13日 9:06
 */
@Api(tags = "PC-医院相关")
@RestController
@RequestMapping("pc/hospital")
@ApiSign(isSign = false)
@Slf4j
public class HospitalController extends BaseController {


	private static final Logger LOGGER = LoggerFactory.getLogger(HospitalController.class);

	@Autowired
	private IHospitalSV ihospitalSV;



	@Autowired
	private IChinaMedicineSV iChinaMedicineSV;

	@Autowired
	private IPensionInfoSV iPensionInfoSV;

	@Autowired
	private IMedicalCentreSV iMedicalCentreSV;
//
//	@Autowired
//	private ICommonSV commonSV;





	@Log
	@ApiOperation("医院新增")
	@PostMapping("/addH")
	@ResponseBody
	public APIResponse addHospital(
			@ApiParam(name = "jointParam", value = "医院信息", required = true)
			@RequestBody HospitalJointParam jointParam) {
		long rows = 0;
		HospitalEntity hospital = jointParam.getHospitalAddParam().covert();
		List<DataMainPicpParam> picIds = jointParam.getDataMainPics();
		List<RegistryWayParam> registryWay = jointParam.getRegistryWayParam();
		hospital.setCreated(ShiroUtil.getLoginId());
		//hospital.setCreSb(1L);
		rows = ihospitalSV.addH(hospital, registryWay, picIds);
		if (rows == 0) {
			return APIResponse.fail("添加失败");
		}
		return APIResponse.successWithNoData("添加成功");
	}


	@Log
	@ApiOperation("医院修改")
	@PostMapping("/updateH")
	@ResponseBody
	public APIResponse upHospital(
			@ApiParam(name = "jointParam", value = "修改医院信息的对象", required = true)
			@RequestBody HospitalJointParam jointParam) {
		HospitalEntity hospital = jointParam.getHospitalEditParam().covert();
		List<DataMainPicpParam> pics = jointParam.getDataMainPics();
		hospital.setUpdated(ShiroUtil.getLoginId());
		List<RegistryWayParam> wayList = jointParam.getRegistryWayParam();
		long rows = ihospitalSV.updateOrInsert(hospital, wayList, pics);

		if (rows == 0) {
			return APIResponse.fail("修改失败");
		}
		return APIResponse.successWithNoData("修改成功");
	}

	@Log
	@ApiOperation("医院删除")
	@PostMapping("/deleteH")
	@ResponseBody
	public APIResponse deHospital(
			@ApiParam(name = "ids数组", value = "ids", required = true)
			@RequestBody Long[] ids) {
		int rows = ihospitalSV.deleteByIds(ids);
		if (rows == 0) {
			return APIResponse.fail("删除失败");
		}
		return APIResponse.successWithNoData("删除成功");

	}

	@Log
	@ApiOperation("医院列表")
	@GetMapping("/list")
	@ResponseBody
	public APIResponse<PageInfo<HospitalVo>> hospitalList(
			@RequestParam(required = false, defaultValue = "1") int pageNo,
			@RequestParam(required = false, defaultValue = "10") int limit,
			@ApiParam(name = "hospitalQryParam", value = "医院信息", required = true)
					HospitalQueryParam hospitalQryParam) {
		PageInfo<HospitalVo> pageHospital = ihospitalSV.hospitalList(pageNo, limit, hospitalQryParam);
		return APIResponse.success(pageHospital);
	}


	@Log(printBody = false)
	@ApiOperation("医院id查询医院详情")
	@GetMapping("/detail")
	@ResponseBody
	public APIResponse<HospitalDetailVo> hospitalDetail(
			@ApiParam(name = "id", value = "医院id", required = true)
			@RequestParam Long id) {
		HospitalDetailVo detailVo = ihospitalSV.pcResultMap(id);
		return APIResponse.success(detailVo);
	}

	@Log(printBody = false)
	@ApiOperation("医院排名列表")
	@GetMapping("/ranking/list")
	@ResponseBody
	public APIResponse<PageInfo<HospitalVo>> rankList(
			@RequestParam(defaultValue = "1", required = false) int pageNo,
			@RequestParam(defaultValue = "10", required = false) int limit,
			@ApiParam(name = "hospitalQryParam", value = "医院信息", required = true)
					HospitalQueryParam hospitalQryParam) {
		PageInfo<HospitalVo> pageHospital = ihospitalSV.hospitalRankList(pageNo, limit, hospitalQryParam);
		return APIResponse.success(pageHospital);
	}

	@Log
	@ApiOperation("医院排名修改")
	@PostMapping("/update/rank/H")
	@ResponseBody
	public APIResponse upRankHospital(
			@ApiParam(name = "hospitalEditParam", value = "修改医院信息的对象", required = true)
			@RequestBody HospitalRankEditParam hospitalEditParam) {
		int rows = ihospitalSV.updateByMyId(hospitalEditParam);
		if (rows == 0) {
			return APIResponse.fail("修改失败");
		}
		return APIResponse.successWithNoData("修改成功");
	}

	@ApiOperation("医院json导入")
	@PostMapping("/json/import")
	@ResponseBody
	public APIResponse hospitalJsonImport(
			@ApiParam(name = "jsonFile", value = "文件", required = true)
					MultipartFile jsonFile) {
		if (jsonFile.isEmpty()) {
			return failMsg("文件不存在");
		}
		JSONObject jsonObject = ImportJsonUtil.jsonFile(jsonFile);
		JSONArray jsonArray = jsonObject.getJSONArray("hospitals");
		long rows = 0L;
		for (int i = 0; i < jsonArray.size(); i++) {
			HospitalEntity hospital = JSONObject.parseObject(jsonArray.get(i).toString(), HospitalEntity.class);
			rows = ihospitalSV.insert(hospital);
		}

		if (rows == 0) {
			return APIResponse.fail("导入失败");
		}
		return APIResponse.successWithNoData("导入成功");

	}

	@ApiOperation("医院名下拉id,name)")
	@GetMapping("/hospitalNames")
	@ResponseBody
	public APIResponse<List<?>> hospitalNames(
			@ApiParam(name = "theBelong", value = "归属code", required = true)
			@RequestParam String theBelong) {
		//医院
		switch (theBelong) {
			case "01":
				TK tk = TK.select(HospitalEntity.class).column("id,hos_name")
						.where().andEqualTo(HospitalEntity::getDeleted, FlagEnum.NO.getValue()).end();
				List<HospitalEntity> hospitalNames = ihospitalSV.selectList(tk);
				return APIResponse.success(hospitalNames);
			//中药堂
			case "02":
				TK tk2 = TK.select(ChinaMedicineEntity.class).column("id,the_name")
						.where().andEqualTo(ChinaMedicineEntity::getDeleted, FlagEnum.NO.getValue()).end();
				List<ChinaMedicineEntity> chinaMedicines = iChinaMedicineSV.selectList(tk2);
				return APIResponse.success(chinaMedicines);
			//体检
			case "03":

				TK tk3 = TK.select(ChinaMedicineEntity.class).column("id,the_name")
						.where().andEqualTo(ChinaMedicineEntity::getDeleted, FlagEnum.NO.getValue()).end();
				List<MedicalCentreEntity> medicalCentres = iMedicalCentreSV.selectList(tk3);
				return APIResponse.success(medicalCentres);
				//养老
			case "04":
				TK tk4 = TK.select(PensionInfoEntity.class).column("id,the_name")
						.where().andEqualTo(PensionInfoEntity::getDeleted, FlagEnum.NO.getValue()).end();
				List<PensionInfoEntity> pensionInfos = iPensionInfoSV.selectList(tk4);
				return APIResponse.success(pensionInfos);
			default:
				return null;
		}
	}

	@ApiOperation("医院发布/下线")
	@PostMapping("/publish/offline")
	@ResponseBody
	public APIResponse upStatues(
			@ApiParam(name = "updateParam", value = "下线接参", required = true)
			@RequestBody HospitalPcUpdateParam updateParam) {
		HospitalEntity hospital = updateParam.covert();
		hospital.setPublisher(ShiroUtil.getLoginId());
		hospital.setPublishTime(new Date());
		int rows = ihospitalSV.updateById(hospital);

		if (rows == 0) {
			return APIResponse.fail("操作失败");
		}
		return APIResponse.successWithNoData("操作成功");

	}


//
//    @ApiOperation("医院索引全量重建")
//    @PostMapping("/rebuildIndex")
//    @ResponseBody
//    public APIResponse<Void> rebuildIndex() throws Exception {
//        List<Hospital> hospitals = ihospitalSV.selectByModel(Query.init(Hospital.class).get());
//        luceneIndex.init();
//        return success();
//    }


}
